import java.util.ArrayList;


public class Productions {

	private ArrayList<Production> leftHandSide;
	private ArrayList<String> keys;
	
	public Productions(){
		
		this.leftHandSide= new ArrayList<Production>();
		this.keys= new ArrayList<String>();
	}
	 
	public ArrayList<String> getKeys(){
		
		return this.keys;
	}
	
	public ArrayList<Production> getLHS(){
		
		return this.leftHandSide;
	}
	
	public Production getProductionByKey(String key){
		
		return this.leftHandSide.get(this.FindKeyPosition(key));
	}
	
	public String getAllProduction(){
		
		String tmp= "";
		for(Production l : this.leftHandSide){
			
			tmp+= l.getKey()+"->";
			for(String a : l.getLHS()){
				
				tmp+= a;
			}
			
			tmp+= ";";
		}
		
		return tmp;
	}
	
	public void addProduction(String key, String production){
		
		if(this.isPresent(key)){
			
			int pos= this.FindKeyPosition(key);
			Production tmp= this.leftHandSide.get(pos);
			tmp.addProduction("|");
			tmp.addProduction(production);
		}
		else{
			
			this.keys.add(key);
			Production tmp= new Production(production, "|", key);
			this.leftHandSide.add(tmp);
		}
	}
	
	public boolean isPresent(String key){
		
		return this.keys.contains(key);
	}
	
	public int FindKeyPosition(String key){
		
		for(int i=0; i<this.leftHandSide.size(); i++){
			
			String tmp= this.leftHandSide.get(i).getKey();
			if(tmp.equals(key)) return i;			
		}
		
		return -1;//in caso di errore
	}
	
	public void setKeys(ArrayList<String> keys){
		
		this.keys= keys;
	}
	
	public void addKey(String key){
		
		this.keys.add(key);
	}
	
	public void removeKey(String key){
		
		this.keys.remove(key);
	}
	
	public void removeAllProductionByKey(String key){
		
		this.leftHandSide.remove(this.FindKeyPosition(key));
	}
}
